/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 *
 * Contributor(s):
 *
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
 */
package org.nbphpcouncil.modules.php.ci.ui.wizards;

import java.awt.Component;
import java.awt.Cursor;
import java.io.File;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.options.OptionsDisplayer;
import org.nbphpcouncil.modules.php.ci.CIPhpFramework;
import org.nbphpcouncil.modules.php.ci.ui.options.CIEntry;
import org.nbphpcouncil.modules.php.ci.ui.options.CIOptions;
import org.openide.util.ChangeSupport;
import org.openide.util.NbBundle;

/**
 *
 * @author Junji Takakura
 */
public class NewProjectConfigurationPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private final DefaultComboBoxModel<CIEntry> baseFileComboBoxModel = new DefaultComboBoxModel<CIEntry>();
    private final DefaultComboBoxModel<CIEntry> languagePackComboBoxModel = new DefaultComboBoxModel<CIEntry>();
    private final ChangeSupport cs = new ChangeSupport(this);

    public NewProjectConfigurationPanel() {
        initComponents();
        initFileComboBoxes();

        optionsLabel.setMaximumSize(optionsLabel.getPreferredSize());
        CIOptions.getInstance().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                initFileComboBoxes();
                cs.fireChange();

            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        optionsLabel = new javax.swing.JLabel();
        generateProjectLabel = new javax.swing.JLabel();
        baseFileLabel = new javax.swing.JLabel();
        baseFileComboBox = new javax.swing.JComboBox();
        baseFileComboBox.setRenderer(new EntityCellRenderer(baseFileComboBox.getRenderer()));
        excludingUserGuideFilesLabel = new javax.swing.JLabel();
        excludingUserGuideFilesCheckBox = new javax.swing.JCheckBox();
        languagePackLabel = new javax.swing.JLabel();
        languagePackComboBox = new javax.swing.JComboBox();
        languagePackComboBox.setRenderer(new EntityCellRenderer(languagePackComboBox.getRenderer()));

        optionsLabel.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.optionsLabel.text")); // NOI18N
        optionsLabel.setToolTipText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.optionsLabel.toolTipText")); // NOI18N
        optionsLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                optionsLabelMouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                optionsLabelMousePressed(evt);
            }
        });

        generateProjectLabel.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.generateProjectLabel.text")); // NOI18N

        baseFileLabel.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.baseFileLabel.text")); // NOI18N

        baseFileComboBox.setModel(baseFileComboBoxModel);

        excludingUserGuideFilesLabel.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.excludingUserGuideFilesLabel.text")); // NOI18N

        excludingUserGuideFilesCheckBox.setSelected(true);
        excludingUserGuideFilesCheckBox.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.excludingUserGuideFilesCheckBox.text")); // NOI18N

        languagePackLabel.setText(org.openide.util.NbBundle.getMessage(NewProjectConfigurationPanel.class, "NewProjectConfigurationPanel.languagePackLabel.text")); // NOI18N
        languagePackLabel.setVisible(false);

        languagePackComboBox.setModel(languagePackComboBoxModel);
        languagePackComboBox.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(generateProjectLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 324, Short.MAX_VALUE)
                .addComponent(optionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(languagePackLabel)
                            .addComponent(baseFileLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(languagePackComboBox, 0, 385, Short.MAX_VALUE)
                            .addComponent(baseFileComboBox, 0, 385, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(excludingUserGuideFilesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(excludingUserGuideFilesCheckBox)
                        .addGap(0, 282, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(generateProjectLabel)
                    .addComponent(optionsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(baseFileLabel)
                    .addComponent(baseFileComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(excludingUserGuideFilesLabel)
                    .addComponent(excludingUserGuideFilesCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(languagePackLabel)
                    .addComponent(languagePackComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void optionsLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_optionsLabelMouseEntered
        evt.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}//GEN-LAST:event_optionsLabelMouseEntered

    private void optionsLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_optionsLabelMousePressed
        OptionsDisplayer.getDefault().open(CIPhpFramework.getOptionsPath());
}//GEN-LAST:event_optionsLabelMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox baseFileComboBox;
    private javax.swing.JLabel baseFileLabel;
    private javax.swing.JCheckBox excludingUserGuideFilesCheckBox;
    private javax.swing.JLabel excludingUserGuideFilesLabel;
    private javax.swing.JLabel generateProjectLabel;
    private javax.swing.JComboBox languagePackComboBox;
    private javax.swing.JLabel languagePackLabel;
    private javax.swing.JLabel optionsLabel;
    // End of variables declaration//GEN-END:variables

    public void addChangeListener(ChangeListener listener) {
        cs.addChangeListener(listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        cs.removeChangeListener(listener);
    }

    public String getErrorMessage() {
        CIEntry entry = getBaseFileEntry();
        String message = null;

        if (entry == null) {
            message = getMessage("MSG_NoSelectedFile", "Base File"); //NOI18N
        } else if (!new File(entry.getPath()).isFile()) {
            message = getMessage("MSG_SelectedFileIsNotFile", "Base File"); //NOI18N
        }

        return message;
    }

    public String getWarningMessage() {
        return null;
    }

    public CIEntry getBaseFileEntry() {
        return (CIEntry) baseFileComboBox.getSelectedItem();
    }

    public CIEntry getLanguagePackEntry() {
        return (CIEntry) languagePackComboBox.getSelectedItem();
    }

    public boolean isExcludingUserGuideFilesSelected() {
        return excludingUserGuideFilesCheckBox.isSelected();
    }

    private void initFileComboBoxes() {
        initBaseFileComboBox();
        initLanguagePackFileComboBox();
    }

    private void initBaseFileComboBox() {
        List<CIEntry> entries = CIOptions.getInstance().getEntries(CIEntry.Type.BASE);

        baseFileComboBoxModel.removeAllElements();

        for (CIEntry entry : entries) {
            baseFileComboBoxModel.addElement(entry);
        }
    }

    private void initLanguagePackFileComboBox() {
        List<CIEntry> entries = CIOptions.getInstance().getEntries(CIEntry.Type.LANGUAGE_PACK);

        languagePackComboBoxModel.removeAllElements();

        for (CIEntry entry : entries) {
            languagePackComboBoxModel.addElement(entry);
        }
    }

    private String getMessage(String resourceName, Object... parameters) {
        return NbBundle.getMessage(NewProjectConfigurationPanel.class, resourceName, parameters);
    }

    private class EntityCellRenderer implements ListCellRenderer {

        private final ListCellRenderer renderer;

        public EntityCellRenderer(final ListCellRenderer renderer) {
            this.renderer = renderer;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof CIEntry) {
                value = ((CIEntry) value).getName();
            }

            return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    }
}
